package 代码随想录.栈与队列;

import java.util.Stack;

public class _1用栈实现队列 {
    /**
     * 232. 用栈实现队列
     * https://leetcode.cn/problems/implement-queue-using-stacks/
     * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）：
     */
    class MyQueue {
        Stack<Integer> s1 ;
        Stack<Integer> s2 ;

        public MyQueue() {
            this.s1 = new Stack<Integer>();
            this.s2 = new Stack<Integer>();
        }

        public void push(int x) {
            s1.push(x);

        }

        public int pop() {
            while(!s1.isEmpty())
                s2.push(s1.pop());
            int val = s2.pop();
            while(!s2.isEmpty())
                s1.push(s2.pop());
            return val;
        }

        public int peek() {
            while(!s1.isEmpty())
                s2.push(s1.pop());
            int val = s2.peek();
            while(!s2.isEmpty())
                s1.push(s2.pop());
            return val;
        }

        public boolean empty() {
            return s1.isEmpty() && s2.isEmpty();
        }
    }
}
